import Cookies from 'js-cookie'
import {
  ACCESS_TOKEN_KEY,
  REFRESH_TOKEN_KEY,
  USER_INFO,
  OPENID_KEY,
  POSITION_KEY,
  ADDRESS_KEY
} from './config'

/* token */
const getAccessTokenStorage = () => Cookies.get(ACCESS_TOKEN_KEY)
const setAccessTokenStorage = (token = '') => Cookies.set(ACCESS_TOKEN_KEY, token, { expires: 7 })
const removeAccessTokenStorage = () => Cookies.remove(ACCESS_TOKEN_KEY)

const getRefreshTokenStorage = () => Cookies.get(REFRESH_TOKEN_KEY)
const setRefreshTokenStorage = (token = '') => Cookies.set(REFRESH_TOKEN_KEY, token, { expires: 7 })
const removeRefreshTokenStorage = () => Cookies.remove(REFRESH_TOKEN_KEY)

const getUserInfoStorage = () => JSON.parse(localStorage.getItem(USER_INFO))
const setUserInfoStorage = (data) => localStorage.setItem(USER_INFO, JSON.stringify(data))
const removeUserInfoStorage = () => localStorage.removeItem(USER_INFO)

/* openid */
const setOpenidCookie = (id) => Cookies.set(OPENID_KEY, id)
const getOpenidCookie = () => Cookies.get(OPENID_KEY)

const setUserPositionStorage = (data) => localStorage.setItem(POSITION_KEY, JSON.stringify(data))
const getUserPositionStorage = () => JSON.parse(localStorage.getItem(POSITION_KEY))

const setUserAddressStorage = (data) => localStorage.setItem(ADDRESS_KEY, JSON.stringify(data))
const getUseAddressStorage = () => JSON.parse(localStorage.getItem(ADDRESS_KEY))

/* beforeLoginUrl */
// const setBeforeLoginUrl = (url) => localStorage.setItem(BEFORE_URL_KEY, url)
// const getBeforeLoginUrl = () => localStorage.getItem(BEFORE_URL_KEY)
// const removeBeforeLoginUrl = () => localStorage.removeItem(BEFORE_URL_KEY)
// const setBeforeLoginUrl = (url) => Cookies.set(BEFORE_URL_KEY, url)
// const getBeforeLoginUrl = () => Cookies.get(BEFORE_URL_KEY)
// const removeBeforeLoginUrl = () => Cookies.remove(BEFORE_URL_KEY)

export {
  getAccessTokenStorage, setAccessTokenStorage, removeAccessTokenStorage,
  getRefreshTokenStorage, setRefreshTokenStorage, removeRefreshTokenStorage,
  getUserInfoStorage, setUserInfoStorage, removeUserInfoStorage,
  setOpenidCookie, getOpenidCookie,
  setUserPositionStorage, getUserPositionStorage,
  setUserAddressStorage, getUseAddressStorage
}
